package gr.handmademarket.ui.panels;

import gr.handmademarket.bl.operations.LoginOperation;
import gr.handmademarket.po.implementations.Buyer;
import gr.handmademarket.po.interfaces.User;
import gr.handmademarket.ui.WebHandMadeSession;
import gr.handmademarket.ui.modals.LoginModal;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

/**
 * the login panel.
 */
public class LoginPanel 
extends Panel{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the form containing the user credentials.
	 */
	@SuppressWarnings("nls")
	private Form<User> form = new Form<User>("uform", new CompoundPropertyModel<User>(new Buyer()));
	
	/**
	 * the username field.
	 */
	@SuppressWarnings("nls")
	private TextField<String> usernameField = new TextField<String>("username");
	
	/**
	 * the password field.
	 */
	@SuppressWarnings("nls")
	private TextField<String> passwordField = new TextField<String>("password");
	
	/**
	 * the cancel button.
	 */
	@SuppressWarnings("nls")
	private AjaxButton cancel = new AjaxButton("cancelBtn", form) {
		
		/**
		 * serialVersionUID
		 */
		private static final long serialVersionUID = 1L;

		@Override
		protected void onSubmit(AjaxRequestTarget target, Form<?> form1) {
			LoginModal modal = (LoginModal) LoginPanel.this.getParent();
			modal.close(target);
		}
	};
	
	/**
	 * submit link.
	 */
	@SuppressWarnings("nls")
	private AjaxButton submit = new AjaxButton("singInBtn", form) {

		/**
		 * serialVersionUID.
		 */
		private static final long serialVersionUID = 1L;

		@Override
		protected void onSubmit(AjaxRequestTarget target, Form<?> form1) {
			if (form.getModelObject().getUsername() == null || form.getModelObject().getPassword() == null ){
				error("user not authenticated");
				target.focusComponent(LoginPanel.this.get("feedbackPanel"));
			} else {
				LoginOperation op = new LoginOperation();
				op.setUsername(form.getModelObject().getUsername());
				op.setPassword(form.getModelObject().getPassword());
				op.execute();
				User user = op.getUser();
				if (user != null){
					WebHandMadeSession session = (WebHandMadeSession) WebHandMadeSession.get();
					session.setUser(user);
					LoginModal modal = (LoginModal) LoginPanel.this.getParent();
					modal.close(target);
				} else{
					error("user not authenticated");
					target.focusComponent(LoginPanel.this.get("feedbackPanel"));
				}
			}
		}
	};

	/**
	 * Creates a new LoginPanel object. 
	 *
	 * @param id
	 */
	@SuppressWarnings("nls")
	public LoginPanel(String id) {
		super(id);
		add(new FeedbackPanel("feedbackPanel").setOutputMarkupId(true));
		add(form);
		form.add(usernameField,passwordField,submit,cancel);
	}


}
